<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/5/11
 * Time: 14:57
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\lib;

use Htlove\tool\Singleton;
use think\Exception;

class Spring
{
    use Singleton;

    protected string $public_key = "";
    protected string $private_key = "";
    protected int $time = 0;
    protected string $url = "https://www.gongyeyuniot.com";

    public function __construct(string $public_key = "", string $private_key = "")
    {
        //"cb7a2e5ef03b41ffbc0577ee0d92abc7","d07ef521ae46484bb0d021cfc1bd63a3"
        if (empty($public_key)) {
            $this->public_key = "cb7a2e5ef03b41ffbc0577ee0d92abc7";
        } else {
            $this->public_key = $public_key;
        }
        if (empty($private_key)) {
            $this->private_key = "d07ef521ae46484bb0d021cfc1bd63a3";
        } else {
            $this->private_key = $private_key;
        }
        $this->time = time();
    }

    /**
     * @return string
     */
    protected function __Token(): string
    {
        $str = "PubKey={$this->public_key}&TS={$this->time}&TTL=1800";
        $sign = hash_hmac("sha1", $str, $this->private_key, true);
        $string_sign = base64_encode($sign);
        return urlencode($string_sign);
    }

    /**
     * @param string $devState 联网状态 -1忽略联网状态
     * @param string $currPage 当前页
     * @param string $perPage 每页数量
     * @return mixed
     * @throws Exception
     */
    public function getMachine(string $devState = "-1", string $currPage = "1", string $perPage = "10")
    {
        $path = "/api/v1/dev";
        $data = [
            "currPage" => $currPage,
            "devState" => $devState,
            "perPage" => $perPage,
        ];
        $head = [
            "pubKey:{$this->public_key}",
            "SIG:{$this->__Token()}",
            "TS:{$this->time}",
            "TTL:1800",

        ];
        $res = Http::getInstance()->get($this->url . $path, $data, $head);

        if ($res['meta']['success']) { //获取成功
            return $res;
        } else {
            return $this->error($res);
        }
    }

    public function pushMachine(string $snCode = "", string $dataCode = "", int $value = 1)
    {
        $path = "/api/v1/dev/rtd/write";
        $data = [
            "dataCode" => $dataCode,
            "value" => $value,
            "snCode" => $snCode
        ];
        $head = [
            "pubKey:{$this->public_key}",
            "SIG:{$this->__Token()}",
            "TS:{$this->time}",
            "TTL:1800",

        ];
        $res = Http::getInstance()->post($this->url . $path, $data, $head);

        if ($res['meta']['success']) { //获取成功
            return $res['data'];
        } else {
            return $this->error($res);
        }
    }


    /**
     * @param string $snCode
     * @param string $currPage
     * @param string $perPage
     * @return mixed
     * @throws Exception
     */
    public function getMachineConfig(string $snCode = "", string $currPage = "1", string $perPage = "100")
    {
        $path = "/api/v1/dev/rtd/config";
        $data = [
            "currPage" => $currPage,
            "perPage" => $perPage,
            "snCode" => $snCode
        ];
        $head = [
            "pubKey:{$this->public_key}",
            "SIG:{$this->__Token()}",
            "TS:{$this->time}",
            "TTL:1800",
        ];
        $res = Http::getInstance()->get($this->url . $path, $data, $head);

        if ($res['meta']['success']) { //获取成功
            return $res;
        } else {
            return $this->error($res);
        }
    }

    /**
     * @param array $res
     * @return mixed
     * @throws Exception
     */
    protected function error(array $res = [])
    {
        switch ($res['meta']['message']) {
            case "120011":
                throw new Exception("超出查询次数");
                break;
            default:
                throw new Exception("查询失败");
                break;
        }
    }

    /**
     * @param string $snCode
     * @param string $dataCode
     * @return mixed
     * @throws Exception
     */
    public function getWriteByList(string $snCode = "", string $dataCode = "")
    {
        $path = "/api/v1/dev/rtd/data";
        $data = [
            "dataCode" => $dataCode,
            "snCode" => $snCode
        ];
        $head = [
            "pubKey:{$this->public_key}",
            "SIG:{$this->__Token()}",
            "TS:{$this->time}",
            "TTL:1800",
        ];
        $res = Http::getInstance()->get($this->url . $path, $data, $head);

        if ($res['meta']['success']) { //获取成功
            return $res['data'];
        } else {
            return $this->error($res);
        }
    }

}